import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
import { defineConfig, loadEnv, ConfigEnv, UserConfig } from "vite";
import vueSetupExtend from 'vite-plugin-vue-setup-extend-plus';
import viteCompression from 'vite-plugin-compression';
import { buildConfig } from './src/utils/build';

const pathResolve = (dir: string) => {
	return resolve(__dirname, '.', dir);
};

const alias: Record<string, string> = {
	// '@': pathResolve('./src'),
	"@": resolve(__dirname, "./src"),
	'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
};

const viteConfig = defineConfig((mode: ConfigEnv) => {
	const env = loadEnv(mode.mode, process.cwd());
	
	return {
		plugins: [vue(), vueSetupExtend(), viteCompression(), JSON.parse(env.VITE_OPEN_CDN) ? buildConfig.cdn() : null],
		root: process.cwd(),
		resolve: { alias },
		base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
		esbuild: {
			pure: env.VITE_DROP_CONSOLE ? ["console.log", "debugger"] : []
		},
		optimizeDeps: { 
			exclude: ['vue-demi'],
			include: [
				"vue",
				"vue-router",
				"pinia",
				"axios",
				"element-plus/es/components/form/style/css",
				"element-plus/es/components/form-item/style/css",
				"element-plus/es/components/button/style/css",
				"element-plus/es/components/input/style/css",
				"element-plus/es/components/input-number/style/css",
				"element-plus/es/components/switch/style/css",
				"element-plus/es/components/upload/style/css",
				"element-plus/es/components/menu/style/css",
				"element-plus/es/components/col/style/css",
				"element-plus/es/components/icon/style/css",
				"element-plus/es/components/row/style/css",
				"element-plus/es/components/tag/style/css",
				"element-plus/es/components/dialog/style/css",
				"element-plus/es/components/loading/style/css",
				"element-plus/es/components/radio/style/css",
				"element-plus/es/components/radio-group/style/css",
				"element-plus/es/components/popover/style/css",
				"element-plus/es/components/scrollbar/style/css",
				"element-plus/es/components/tooltip/style/css",
				"element-plus/es/components/dropdown/style/css",
				"element-plus/es/components/dropdown-menu/style/css",
				"element-plus/es/components/dropdown-item/style/css",
				"element-plus/es/components/sub-menu/style/css",
				"element-plus/es/components/menu-item/style/css",
				"element-plus/es/components/divider/style/css",
				"element-plus/es/components/card/style/css",
				"element-plus/es/components/link/style/css",
				"element-plus/es/components/breadcrumb/style/css",
				"element-plus/es/components/breadcrumb-item/style/css",
				"element-plus/es/components/table/style/css",
				"element-plus/es/components/tree-select/style/css",
				"element-plus/es/components/table-column/style/css",
				"element-plus/es/components/select/style/css",
				"element-plus/es/components/option/style/css",
				"element-plus/es/components/pagination/style/css",
				"element-plus/es/components/tree/style/css",
				"element-plus/es/components/alert/style/css",
				"element-plus/es/components/radio-button/style/css",
				"element-plus/es/components/checkbox-group/style/css",
				"element-plus/es/components/checkbox/style/css",
				"element-plus/es/components/tabs/style/css",
				"element-plus/es/components/tab-pane/style/css",
				"element-plus/es/components/rate/style/css",
				"element-plus/es/components/date-picker/style/css",
				"element-plus/es/components/notification/style/css",
				"element-plus/es/components/image/style/css",
				"element-plus/es/components/statistic/style/css",
				"element-plus/es/components/watermark/style/css",
				"element-plus/es/components/config-provider/style/css",
				// "@vueuse/core",
				"sortablejs",
				// "path-to-regexp",
				"echarts",
				"@wangeditor/editor",
				"@wangeditor/editor-for-vue",
				"vue-i18n"
			] 
		},
		server: {
			host: '0.0.0.0',
			port: env.VITE_PORT as unknown as number,
			open: JSON.parse(env.VITE_OPEN),
			hmr: true,
			cors: true,
			proxy: {
				[env.VITE_API_URL]: {
					target: env.VITE_API_TARGET,
					ws: true,
					changeOrigin: true,
					rewrite: (path) => path.replace(/^\/api/, ''),
				},
			},
		},
		
		build: {
			outDir: 'dist',
			// minify: "esbuild",
			chunkSizeWarningLimit: 1500,
			rollupOptions: {
				output: {
					chunkFileNames: 'assets/js/[name]-[hash].js',
					entryFileNames: 'assets/js/[name]-[hash].js',
					assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
					manualChunks(id) {
						if (id.includes('node_modules')) {
							return id.toString().match(/\/node_modules\/(?!.pnpm)(?<moduleName>[^\/]*)\//)?.groups!.moduleName ?? 'vender';
						}
					},
				},
				...(JSON.parse(env.VITE_OPEN_CDN) ? { external: buildConfig.external } : {}),
			},
		},
		css: { preprocessorOptions: { css: { charset: false } } },
		define: {
			__VUE_I18N_LEGACY_API__: JSON.stringify(false),
			__VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
			__INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
			__NEXT_VERSION__: JSON.stringify(process.env.npm_package_version),
			__NEXT_NAME__: JSON.stringify(process.env.npm_package_name),
		},
	};
});

export default viteConfig;
